package com.thj.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.commons.po.RolePo;
import com.dao.BaseDao;
import com.thj.dao.RoleDao;


public class RoleDaoImpl extends BaseDao<RolePo> implements RoleDao {

	@Override
	public List<RolePo> findByCondition(Map<String, Object> map) {
		StringBuffer sf = new StringBuffer();
		sf.append(" select * from role where 1=1 ");
		Map<String, String> columnsMap = new HashMap<String, String>();
		 columnsMap.put("is_active", "isactive");//����������������ͬʱ
		return this.executeQuery(sf.toString(), columnsMap, RolePo.class, null);
	}

	@Override
	public RolePo findbyId(Integer id) {
		String sql = "select * from role where role_id=?";
		Map<String, String> columnsMap = new HashMap<String, String>();
		columnsMap.put("role_id", "id");// ����������������ͬʱ
		List<RolePo> list = this.executeQuery(sql, columnsMap, RolePo.class, id);
		return (list != null && list.size() > 0) ? list.get(0) : null;
	}

	@Override
	public int updateRolePo(RolePo rolePo) {
		//UPDATE ������ SET ������ = ��ֵ WHERE ������ = ĳֵ
		//role_id, role_name, role_introduce, created_id, created_date, updated_id, updated_date, is_active;
		//public int executeUpdate(String sql,Object... paramsObjs)
//		String sql = "update role(role_name, role_introduce, created_id, created_date, updated_id, updated_date, is_active) "
//				+ " values(?, ?,?, ?,?, ?,?) where role_id=?";
		
		String sql="update role set role_name=?,role_introduce=?,created_id=?,created_date=?,updated_id=?,updated_date=?,is_active=? where role_id=?";
		//update role set role_name='c1' where role_id=25;
//		String sql="update role set role_name=?  where role_id=?";
		
		List<Object> list = new ArrayList<Object>();
		
		list.add(rolePo.getRoleName());
		list.add(rolePo.getRoleIntroduce());
		list.add(rolePo.getCreatedId());
		list.add(rolePo.getCreatedDate());
		list.add(rolePo.getUpdatedId());
		list.add(rolePo.getUpdatedDate());
		list.add(rolePo.getIsactive());
		list.add(rolePo.getRoleId());
		return executeUpdate(sql, list.toArray());
	}
	
	@Test
	public void test01(){
		RolePo rolePo = new RolePo();
		rolePo.setRoleId(25);
		rolePo.setRoleName("ccc");
		System.out.println(this.findByCondition(null));
		
		int num = updateRolePo(rolePo);
		if(num > 0){
			System.out.println("�ɹ�");
		}else{
			System.out.println("ʧ��");
		}
	}

	@Override
	public int saveRole(RolePo rolePo) {
		String sql="insert into role values(role_sequence.nextval,?,?,?,?,?,?,?)";
		//		String sql="update role set role_name=?,role_introduce=?,created_id=?,created_date=?,
		//updated_id=?,updated_date=?,is_active=? where role_id=?";
		//1��new Object[]
		Object[] paramsObjs={
//				
				rolePo.getRoleName(),
				rolePo.getRoleIntroduce(),
				rolePo.getCreatedId(),
				rolePo.getCreatedDate(),
				rolePo.getUpdatedId(),
				rolePo.getUpdatedDate(),
				rolePo.getIsactive(),

		};
		//2��List
		return this.executeUpdate(sql, paramsObjs);
	}
	
	@Test
	public void test02(){
		RolePo rolePo = new RolePo();
		//rolePo.setRoleId(26);
		rolePo.setRoleName("zhao");
		this.saveRole(rolePo);
	}

	@Override
	public int deleteRole(Integer roleId) {
		System.out.println("delete role is working ...");
		String sql="delete from role where role_id=?";
		return this.executeUpdate(sql, roleId);
	}

	@Override
	public int banRole(Integer roleId) {
		//IS_ACTIVE
		System.out.println("BaseDao is ban the role..");
		String sql = "update role set is_active=0 where role_id=?";
		return this.executeUpdate(sql, roleId);
	}

	@Override
	public int activeRole(Integer roleId) {
		//IS_ACTIVE
		System.out.println("BaseDao is ban the role..");
		String sql = "update role set is_active=1 where role_id=?";
		return this.executeUpdate(sql, roleId);
	}



	@Override
	public int deleteRoleMenu(Integer roleId, Integer menuId) {
		String sql="delete role_menu where role_id=? ";
		List<Object> paramList=new ArrayList<>();
		paramList.add(roleId);
//		paramList.add(menuId);
		return (int) this.executeUpdate(sql, paramList.toArray());
	}

	@Override
	public int saveRoleMenu(Integer roleId, Integer menuId) {
		String sql="insert into role_menu values(?,?) ";
		List<Object> paramList=new ArrayList<>();
		paramList.add(roleId);
		paramList.add(menuId);
		return (int) this.executeUpdate(sql,paramList.toArray());
	}

}
